﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xyz.Framework.Server.DataAcessLayer;
using Xyz.Framework.Shared.Utils;
using Xyz.Framework.Shared.DataContracts.DTO;
using System.Data.Objects;
using Xyz.Framework.Server.DomainObjects;
using Xyz.Framework.Shared.DataContracts.Enums;


namespace Xyz.Framework.Server.Security
{
    public class AuthorizationManagement
    {
        public static Xyz.Framework.Shared.DataContracts.DTO.RoleDetailsDTO[] GetRoles()
        {
            List<RoleDetailsDTO> _result = new List<RoleDetailsDTO>();
            using (DataBaseContext db = new DataBaseContext(ConnectionStringUtility.CONNECTION_STRING, ConnectionStringUtility.CONTAINER_NAME))
            {
                ObjectSet<Role> _roleSet = db.CreateObjectSet<Role>();
                ObjectSet<RoleAuthority> _roleAuthoritySet = db.CreateObjectSet<RoleAuthority>();
                ObjectSet<WebApplication> _webApplicationSet = db.CreateObjectSet<WebApplication>();
                ObjectSet<RoleWebApplication> _roleWebApplicationSet = db.CreateObjectSet<RoleWebApplication>();

                var _query = from r in _roleSet
                             join ra in _roleAuthoritySet on r.Id equals ra.RoleId into authorities
                             //from authority in authorities.DefaultIfEmpty()
                             join rw2 in
                             (
                                 from rw in _roleWebApplicationSet 
                                 join w in _webApplicationSet on rw.WebApplicationId equals w.Id
                                 select new
                                 {
                                     rw.RoleId,
                                     WebApplicationId = w.Id,
                                     WebApplicationName = w.Name,
                                     w.Uri,
                                     w.Enabled,
                                 }
                             ) on r.Id equals rw2.RoleId into webapps
                             //from app in webapps.DefaultIfEmpty()
                             select new
                             {
                                 RoleId = r.Id,
                                 RoleName = r.Name,
                                 authorities,
                                 webapps
                                 //authority,
                                 //app,
                             };

                var _resultquery = _query.ToArray();
                if (_resultquery != null && _resultquery.Count() > 0)
                {
                    foreach (var item in _resultquery)
                    {
                        RoleDetailsDTO _data = new RoleDetailsDTO();
                        _data.RoleId = item.RoleId;
                        _data.RoleName = item.RoleName;

                        //nesne içerisindeki yetkiler dolduruluyor
                        if(item.authorities.Count()>0)
                        {
                            List<EFrameworkAuthority> _authlist = new List<EFrameworkAuthority>();
                            foreach (var AuthorityItem in item.authorities)
                            {
                                _authlist.Add((EFrameworkAuthority)AuthorityItem.AuthorityId);
                            }
                            _data.RoleAuthorities = _authlist.ToArray();
                        }
                        //nesne içerisindeki yetkili uygulama bilgileri dolduruluyor
                        if (item.webapps.Count() > 0)
                        {
                            _data.AuthorizedApplications = (from y in item.webapps
                                                            select new WebApplicationDTO()
                                                            {
                                                                Id = y.WebApplicationId,
                                                                Name = y.WebApplicationName,
                                                                Uri = y.Uri,
                                                                Enabled = y.Enabled
                                                            }).ToArray();
                        }
                        _result.Add(_data);
                    }
                }
                return _result.ToArray();
            }
        }
    }
}
